package com.example.dakotanews.controller;

import com.example.dakotanews.dto.utils.ApiResponse;
import com.example.dakotanews.dto.utils.ResponseCode;
import com.example.dakotanews.entity.User;
import com.example.dakotanews.mapper.UserMapper;
import com.example.dakotanews.utils.TimeHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.DayOfWeek;
import java.time.Duration;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;


/**
 * @description 测试类，测试controller以及获取时间后计算与当前时间的间隔
 */
@RestController
@RequestMapping("/test")
public class TestController {

    private final UserMapper userMapper;
    private final TimeHandler timeHandler;

    @Autowired
    public TestController(
            UserMapper userMapper,
            TimeHandler timeHandler
    ){
        this.userMapper = userMapper;
        this.timeHandler = timeHandler;
    }

    // @PreAuthorize("hasAnyAuthority('select')")   // 该注解对自定义的JWT用户信息类似乎没有用
    @GetMapping("/getTime")
    public ApiResponse<Object> getTimeTest(){
        System.out.println("Test content");
        List<String> stringList = new ArrayList<>();

        // 获取系统当前时间
        LocalDateTime currentTime = LocalDateTime.now();

        // 使用mybatisplus获取全部用户数据
        List<User> users = userMapper.selectList(null);

        for (User user : users) {
            // 计算用户信息的创建时间和系统当前时间的间隔
            Duration duration = Duration.between(user.getCreateTime(), currentTime);

            // 分别获取对应的天数、小时数、分钟数和秒数的间隔
            long durationDays = duration.toDays();        // 整数天数
            long durationHours = duration.toHours() % 24; // 表示不足一天的小时
            long durationMinutes = duration.toMinutes() % 60; // 表示不足一小时的分钟
            long durationSeconds = duration.getSeconds() % 60; // 表示不足一分钟的秒数

            System.out.println(user.getCreateTime().getHour() + ":" + user.getCreateTime().getMinute());

            DayOfWeek dayOfWeek = user.getCreateTime().getDayOfWeek();
            int dayOfMonth = user.getCreateTime().getDayOfMonth();
            int dayOfYear = user.getCreateTime().getDayOfYear();
            int year = user.getCreateTime().getYear();

            System.out.println(user.getId());
            System.out.println(timeHandler.dateTimeConvert(user.getCreateTime()));
//            System.out.println("duration: " + duration);
//            System.out.println(durationDays);
//            System.out.println(durationHours);
//            System.out.println(durationMinutes);
//            System.out.println(durationSeconds);
//            System.out.println("dayOfWeek: " + dayOfWeek);
//            System.out.println("dayOfMonth: " + dayOfMonth);
//            System.out.println("dayOfYear: " + dayOfYear);
//            System.out.println("year: " + year);


            String timeString;

            // 根据间隔的大小，可以显示指定的信息
            if(durationDays < 7){
                timeString = "7天以内";
            }else {
                timeString = durationDays + "天" + durationHours + "小时前";
            }

            stringList.add("id: " + user.getId() + ", nickname: " + user.getNickname() +
                    ", create time: " + user.getCreateTime() + ", time from now: " + timeString);
        }
        return new ApiResponse<>(ResponseCode.SUCCESS, "响应成功", stringList);
    }

    // @PreAuthorize("hasAnyAuthority('update')")
    @GetMapping("/hello")
    public ApiResponse<Object> getHello(){
        return new ApiResponse<>(ResponseCode.SUCCESS, "响应成功", "hello world");
    }

}
